///////////////////////////////////////////////////////////////////////////////////
//
// Writer  : Dong-moon Lim, Dept. of Messenger, SK Communications, Korea
//
// Email   : dmlim@nate.com
//
// Created : 2008-06-05 11:31:40
//
// Description
//		This module is C++ clone of "CAPICOM.EncryptedData" COM Object
//		CAPICOM uses "CAPICOM.EncryptedData" in Microsoft own format,
//		compatible with PKCS7 "Enveloped Data". 
//
// Special thanks to
//		Michel I. Gallant (neutron@istar.ca)
//		Reference: Understanding CAPICOM EncryptedData
//		http://www.jensign.com/JavaScience/dotnet/DeriveBytes/index.html
//
///////////////////////////////////////////////////////////////////////////////////

#ifndef __N_CAPI_COM_H_
#define __N_CAPI_COM_H_

#include <ncrypto.h>
#include <ncrypt_types.h>

namespace NCAPICOM
{
	class EncryptedData
	{
	protected:
		CNCryptoContext* m_pContext;

		char* m_charset;

		LPWSTR	m_contentW;
		LPSTR	m_contentA;

		LPWSTR	m_secretW;
		LPSTR	m_secretA;

		ALG_ID	m_algId;

		void ClearContent();
		void ClearSecret();

	public:
		EncryptedData(CNCryptoContext *pContext, const char* charset = 0);
		~EncryptedData();

		void SetContent(LPCSTR content);
		void SetContent(LPCWSTR content);

		LPCTSTR GetContent();
		LPCSTR GetContentA();
		LPCWSTR GetContentW();

		void SetSecret(LPCSTR secret);
		void SetSecret(LPCWSTR secret);

		LPCTSTR GetSecret();
		LPCSTR GetSecretA();
		LPCWSTR GetSecretW();

		void SetAlgorithm(ALG_ID algId = 0);
		ALG_ID GetAlgorithm();

		char* GenerateHash(const BYTE* pKey, DWORD keySize);

		NBLOB* Encrypt(DWORD blobType = NCRYPT_BLOB_BASE64, BOOL isURL = FALSE);

		BOOL Decrypt(const BYTE* content, int len = -1, DWORD blobType = NCRYPT_BLOB_BASE64);

		BOOL Decrypt(LPCWSTR content, DWORD blobType = NCRYPT_BLOB_BASE64);
		BOOL Decrypt(LPCSTR content, DWORD blobType = NCRYPT_BLOB_BASE64);
	};
}

#endif // __N_CAPI_COM_H_
